package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.constant.MessageConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工接口")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return Result<EmployeeLoginVO>
     */
    @ApiOperation(value = "员工登录")
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);
        Employee employee = employeeService.login(employeeLoginDTO);
        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);
        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 员工分页查询
     *
     * @param employeePageQueryDTO
     * @return Result<PageResult>
     */
    @ApiOperation(value = "员工分页查询")
    @GetMapping("/page")
    public Result<PageResult> searchEmployee(EmployeePageQueryDTO employeePageQueryDTO) {
        log.info("员工分页查询{}", employeePageQueryDTO);
        ArrayList<Employee> employees = new ArrayList<>();
        PageResult pageResult = new PageResult();
        Long count = null;
        employees = employeeService.searchEmployee(employeePageQueryDTO);
        pageResult.setRecords(employees);
        count = employeeService.getCount();
        pageResult.setTotal(count);
        return Result.success(pageResult);
    }

    /**
     * 新增员工
     *
     * @param employeeDTO
     * @return
     */
    @ApiOperation("新增员工")
    @PostMapping()
    public Result<String> addEmp(@RequestBody EmployeeDTO employeeDTO) {
        log.info("新增员工{}", employeeDTO);
        employeeService.addEmp(employeeDTO);
        return Result.success();
    }

    /**
     * 员工帐号状态修改
     *
     * @return
     */
    @ApiOperation("员工帐号状态修改")
    @PostMapping("/status/{status}")
    public Result<String> changeStatus(@PathVariable Integer status, int id) {
        employeeService.changeStatus(status, id);
        return Result.success();
    }

    /**
     * 退出
     *
     * @return
     */
    @ApiOperation(value = "退出登录")
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 修改密码
     *
     * @param passwordEditDTO
     * @return
     */
    @ApiOperation("修改密码")
    @PutMapping("/editPassword")
    public Result<String> changePassword(@RequestBody PasswordEditDTO passwordEditDTO) {
        log.info("修改密码{}", passwordEditDTO);
        int count = employeeService.changePassword(passwordEditDTO);
        //1:成功 2:密码与原密码重复 0:旧密码不正确
        if (count == 1) {
            return Result.success();
        } else if (count == 2) {
            return Result.error(MessageConstant.ACCOUNT_yiyang);
        }
        return Result.error(MessageConstant.PASSWORD_ERROR);
    }

    /**
     * 根据ID查询员工回显
     * @param id
     * @return
     */
    @ApiOperation("根据ID查询员工回显")
    @GetMapping("/{id}")
    public Result<Employee> searchId(@PathVariable int id) {
        log.info("根据id回显员工信息{}",id);
        Employee employee = employeeService.searchId(id);
        return Result.success(employee);
    }

    /**
     * 修改员工信息
     *
     * @param employeeDTO
     * @return
     */
    @ApiOperation("编辑员工信息")
    @PutMapping
    public Result<String> EditorialStaff(@RequestBody EmployeeDTO employeeDTO) {
        log.info("修改员工信息{}", employeeDTO);
        employeeService.changeEmp(employeeDTO);
        return Result.success();
    }
}
